<template>
  <div class="app-container">
    <el-dialog :title="titleList[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
      <el-form ref="form" :model="form" :disabled="flag==1">
        <el-row :gutter="10" style="display: flex;flex-flow: wrap;">
          <el-col :span="8">
            <el-form-item label="单位名称" prop="merchantName" required>
              <el-input v-model="form.merchantName" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户级别" prop="merchantLevel" required>
              <el-select v-model="form.merchantLevel" style="width: 100%">
                <el-option
                  v-for="item in dict.type.sys_merchant_level"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="contactPerson" required>
              <el-input v-model="form.contactPerson" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号" prop="mobile" required>
              <el-input v-model="form.mobile" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位类型" prop="merchantType" required>
              <el-select v-model="form.merchantType" style="width: 100%">
                <el-option
                  v-for="item in dict.type.ims_merchant_type"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.merchantType==1">
            <el-form-item label="对接销售人员" prop="saleUserId" required>
              <el-select v-model="form.saleUserId" filterable style="width: 100%">
                <el-option
                  v-for="item in salerList"
                  :key="item.userId"
                  :label="item.nickName+'（'+item.deptName+'）'"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="8" v-if="form.merchantType==1">
            <el-form-item label="客户账号" prop="userIdList" required>
              <el-select v-model="form.userIdList" multiple filterable style="width: 100%">
                <el-option
                  v-for="item in customerList"
                  :key="item.userId"
                  :label="item.nickName+'（'+item.deptName+'）'"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结算方式" prop="paymentType" required>
              <el-select v-model="form.paymentType" style="width: 100%">
                <el-option
                  v-for="item in dict.type.ims_payment_type"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开户行" prop="bankName" required>
              <el-input v-model="form.bankName" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="银行账户" prop="bankAccount" required>
              <el-input v-model="form.bankAccount" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="税号" prop="taxCode" required>
              <el-input v-model="form.taxCode" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮箱" prop="email" required>
              <el-input v-model="form.email" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位地址" prop="address" required>
              <el-input v-model="form.address" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否开票" prop="taxType" required>
              <el-select v-model="form.taxType" style="width: 100%">
                <el-option
                  v-for="item in dict.type.sys_yes_no"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="赊账期限(天)" prop="creditDays" required>
              <el-input type="number" v-model="form.creditDays" placeholder="请输入" :min="0" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="赊账额度(元)" prop="creditLimit" required>
              <el-input type="number" v-model="form.creditLimit" placeholder="请输入" :min="0" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUnit, updateUnit} from "../../../api/imsMng";
import {listUser} from "../../../api/system/user";

export default {
  dicts: ['ims_payment_type', 'ims_merchant_type', 'sys_merchant_level', 'sys_yes_no'],
  data() {
    return {
      flag: 0,
      loading: false,
      dialogVisible: false,
      form: {
        merchantType: ''
      },
      titleList: ['新增', '详情', '修改'],
      paymentTypeList: [],
      salerList: [],
      customerList: [],
    }
  },
  watch: {
    'form.merchantName'() {
      let req = {
        deptName: this.form.merchantName,
        pageSize: -1
      }
      listUser(req).then(res => {
        this.customerList = res.rows
      })
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.form = {}
      this.resetForm('form')
      this.form = form
      this.load()
    },
    load() {
      let req = {
        deptName: '销售部门',
        pageSize: -1
      }
      listUser(req).then(res => {
        this.salerList = res.rows
      })

    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          if (this.flag == 2) {
            updateUnit(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            })
          } else {
            addUnit(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            })
          }
        }
      })
    },
  }
}
</script>
